package ks.managers.table;

import java.util.Collection;
import java.util.Vector;

/**
 * A class that represents a table in the game room.
 * 
 * @author Harry Moreno
 * @author Christopher Page
 * @author Jared Razee
 */
public class Table {
	
	/**
	 * An enum value specifying the table's status.
	 */
	public enum STATUS {
		/** Players may join the table freely. */ OPEN, 
		/** Players must be accepted to table by the moderator. */ RESTRICTED, 
		/** Players cannot join the table. */ CLOSED
	}
	
	protected int tableId; /**< The number of the table (from 1-100) */
	protected STATUS tableStatus; /**< Status of table */
	protected String moderatorUsername; /**< The moderator's username */
	protected Vector<String> knownUsers; /**< Usernames of users on table */
	
	/**
	 * Constructor
	 * 
	 * @param tableNum The number of the table.
	 */
	public Table (int tableNum){
		this.tableId = tableNum;
		this.moderatorUsername = null;
		this.knownUsers = new Vector<String>(0);
		this.tableStatus = STATUS.OPEN;
	}
	
	/**
	 * Retrieves the id of the table.
	 * 
	 * @return An integer representing the table's id.
	 */
	public int getId() { return tableId; }
	
	/**
	 * Sets the table's status.
	 * 
	 * @param ks.managers.table.Table.STATUS The table's status.
	 */
	public void setStatus(STATUS s) { tableStatus = s; }

	/**
	 * Retrieves the table's status.
	 * 
	 * @return ks.managers.table.Table.STATUS The table's status.
	 */
	public STATUS getStatus() { return tableStatus; }
	
	/**
	 * Returns a collection of players at the table.
	 * 
	 * @return Collection<String>
	 */
	public Collection<String> getPlayers() {
		return knownUsers;
	}
	
	/**
	 * Retrieves the username of the moderator.
	 * 
	 * @return A string representing the username of the moderator.
	 */
	public String getModerator() { return moderatorUsername; }
	
	/**
	 * Sets the moderator to a given username.
	 * 
	 * @param name The username of the user to set as the moderator.
	 */
	public void setModerator(String name) { moderatorUsername = name; }
	
	/**
	 * Retrieves a boolean value stating whether the specified user is at the table.
	 * 
	 * @param username The username of the user to check for.
	 * @return True if the user is at the table; false otherwise.
	 */
	public boolean isAtTable(String username) { return knownUsers.contains(username); }
	
	/**
	 * Retrieves a boolean value stating whether the table is empty or not.
	 * 
	 * @return True if empty. False if not empty.
	 */
	public boolean isEmpty() {
		knownUsers.trimToSize();
		return (knownUsers.size() == 0);
	}
	
	/**
	 * Retrieves a boolean value stating whether the table is full or not.
	 * 
	 * @return True if full. False if not full.
	 */
	public boolean isFull() {
		knownUsers.trimToSize();
		return (knownUsers.size() == 4);
	}
	
	/**
	 * Adds a player to the table.
	 * 
	 * @param name The username of the player to be added.
	 * @return True on success.
	 */
	public boolean addPlayer(String name) {
		knownUsers.trimToSize();
		if (knownUsers.size() < 4)
			return knownUsers.add(name);
		return false;
	}
	
	/** 
	 * Removes a player from the table.
	 * 
	 * @param name The username of the player to be removed.
	 * @return True on success.
	 */
	public boolean removePlayer(String name) {
		if (!knownUsers.remove(name))
			return false;
		return true;
	}
	
	/**
	 * Resets the table to have no players, no moderator, and a status of open.
	 * 
	 * @return True on success.
	 */
	public boolean resetTable() {
		knownUsers = new Vector<String>(0);
		this.moderatorUsername = null;
		this.tableStatus = STATUS.OPEN;
		return true;
	}
}
